package UIComponents
{
	import MyComponent.MyButton;
	
	import fl.events.ComponentEvent;
	
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;

	/**
	 *金币兑换ui 
	 * @author 邓坚
	 * 
	 */	
	public class PanelExChange extends Sprite
	{
		private var _txtJb1:TextField;			//金币框1行
		private var _txtJb2:TextField;			//金币框2行
		private var _txtJb3:TextField;			//金币框3行
		private var _txtOb1:TextField;			//o币框1行
		private var _txtOb2:TextField;			//o币框2行
		private var _txtOb3:TextField;			//o币框3行
		private var _buyBtn1:MyButton;			//兑换1
		private var _buyBtn2:MyButton;			//兑换2
		private var _buyBtn3:MyButton;			//兑换3
		private var _shopItem1:IGD_ShopItem;	//商品1
		private var _shopItem2:IGD_ShopItem;	//商品2
		private var _shopItem3:IGD_ShopItem;	//商品3

		public function PanelExChange()
		{
			init();
		}
		
		public function init():void
		{
			_txtJb1		=	(getChildByName("txt_jb1")	as TextField);
			_txtJb2		=	(getChildByName("txt_jb2")	as TextField);
			_txtJb3		=	(getChildByName("txt_jb3")	as TextField);
			
			_txtOb1		=	(getChildByName("txt_ob1")	as TextField);
			_txtOb2		=	(getChildByName("txt_ob2")	as TextField);
			_txtOb3		=	(getChildByName("txt_ob3")	as TextField);
			
			_buyBtn1	=	(getChildByName("btn1") as MyButton);
			_buyBtn2	=	(getChildByName("btn2") as MyButton);
			_buyBtn3	=	(getChildByName("btn3") as MyButton);
			
			addListener();
		}	
		
		public function addListener():void
		{
			_buyBtn1.addEventListener(ComponentEvent.BUTTON_DOWN,btn1_clickHandler);
			_buyBtn2.addEventListener(ComponentEvent.BUTTON_DOWN,btn2_clickHandler);
			_buyBtn3.addEventListener(ComponentEvent.BUTTON_DOWN,btn3_clickHandler);
			(getChildByName("btn_close") as MyButton).addEventListener(ComponentEvent.BUTTON_DOWN,close_clickHandler);
		}
	
		public function controlMoenyIcon(qb:Boolean=true):void
		{
			var ms1:DisplayObjectContainer=this.getChildByName("moneySp1") as DisplayObjectContainer;
			ms1.getChildByName("qdian").visible=qb;
			ms1.getChildByName("obIcon").visible=!qb;
			
			var ms2:DisplayObjectContainer=this.getChildByName("moneySp2") as DisplayObjectContainer;
			ms2.getChildByName("qdian").visible=qb;
			ms2.getChildByName("obIcon").visible=!qb;
			
			var ms3:DisplayObjectContainer=this.getChildByName("moneySp3") as DisplayObjectContainer;
			ms3.getChildByName("qdian").visible=qb;
			ms3.getChildByName("obIcon").visible=!qb;
		}
		
		
		/**
		 *每1行购买 
		 * @param e
		 * 
		 */
		public function btn1_clickHandler(e:Event):void
		{
			this.dispatchEvent(new PanelEvent("btn1Click",true));
		}
			
		/**
		 *每2行购买  
		 * @param e
		 * 
		 */
		public function btn2_clickHandler(e:Event):void
		{
			this.dispatchEvent(new PanelEvent("btn2Click",true));
		}
			
		/**
		 *每3行购买  
		 * @param e
		 * 
		 */
		public function btn3_clickHandler(e:Event):void
		{
			this.dispatchEvent(new PanelEvent("btn3Click",true));
		}
		
		public function close_clickHandler(e:Event):void
		{
			this.dispatchEvent(new PanelEvent(PanelEvent.CLOSE,true));
		}
		
		public function sendBuyCommand1():void
		{
			this._shopItem1.sendBuyCommand();
		}
		public function sendBuyCommand2():void
		{
			this._shopItem2.sendBuyCommand();
		}
		public function sendBuyCommand3():void
		{
			this._shopItem3.sendBuyCommand();
		}
		
		/**
		 *设置第row行的金币数，和o币数 
		 * @param row
		 * @param ob
		 * @param jb
		 * 
		 */
		public function setJbAndOb(row:uint,ob:Number,jb:Number):void
		{
			this["_txtOb"+row].text	=	ob.toString();
			this["_txtJb"+row].text	=	jb.toString();
		}
		
		
		/**
		 *设置第row行的数据 
		 * @param row
		 * @param shopItem
		 * 
		 */
		public function setData(row:uint,shopItem:IGD_ShopItem):void
		{
			if(row>3 || row<1)throw new Error("索引出错");
			this["_shopItem"+row]	=	shopItem;
			
			setJbAndOb(row,shopItem.price,shopItem.perNum);
			
			if(shopItem.isReachMoney){
				(this["_buyBtn"+row] as MyButton).mouseChildren=true;
				(this["_buyBtn"+row] as MyButton).mouseEnabled=true;
				(this["_buyBtn"+row] as MyButton).buttonMode=true;
	
			}
			else
			{
				(this["_buyBtn"+row] as MyButton).mouseChildren=false;
				(this["_buyBtn"+row] as MyButton).mouseEnabled=false;
				(this["_buyBtn"+row] as MyButton).buttonMode=false;
			}
			
			
		}
		
		public function setDataFromShops(shops:Array):void
		{
			if(shops.length<1)throw new Error("没有找到此商品");
			setData(1,shops[0]);
			setData(2,shops[1]);
			setData(3,shops[2]);
		}

		/**
		 * 释放资源 
		 * @param evt
		 */		
		public function release(evt:Event):void
		{
			this._buyBtn1.removeEventListener(ComponentEvent.BUTTON_DOWN, btn1_clickHandler);
			this._buyBtn2.removeEventListener(ComponentEvent.BUTTON_DOWN, btn2_clickHandler);
			this._buyBtn3.removeEventListener(ComponentEvent.BUTTON_DOWN, btn3_clickHandler);
			//			this.removeEventListener(Event.EXIT_FRAME, relse);
		}
		
		/**
		 * 过滤数据 
		 * @param dataArr  金币或者OB数组
		 * @param itemType 物品类型数组
		 * @return 
		 * 
		 */		
		public function filterData(items:Object,type:uint=100):Array
		{
			var tmpArr:Array=new Array();
			for each(var item:IGD_ShopItem in items)
			{
				if(item.secondType==type)tmpArr.push(item);
			}
			return tmpArr;
		}
		
		/**
		 *设置面板数据 从IGD_shop
		 * @param items
		 * 
		 */
		public function setAllData(items:Object):void
		{
			setDataFromShops(filterData(items));
		}
	}
}